#include "JsonParseUtils.h"
//不需要解析太多，只是一个消息类型就行就可以了
int parseMsgType(const char *data)
{
    cJSON *json =cJSON_Parse(data);
    if (!json)
    {
        //出错了
        printf("Error before: [%s]\n",cJSON_GetErrorPtr());
        return -1;
    }else
    {
        int type = cJSON_GetObjectItem(json,"msgType")->valueint;
        cJSON_Delete(json);
        return type;
    }
}

int parseMsgData(THData *thdata,const char *dataMsg)
{
    cJSON *json =cJSON_Parse(dataMsg);
    cJSON *data;//装content内容
    //char *data="{\"msgType\":1,\"content\":{\"state\":15,\"temp\":20,\"humi\":30,\"somke\":60,\"suns\":80}}";
    if (!json)
    {
        //出错了
        printf("Error before: [%s]\n",cJSON_GetErrorPtr());
        return -1;
    }else
    {
        data =cJSON_GetObjectItem(json,"content");
        //解析内部数据
        thdata->state=cJSON_GetObjectItem(data,"state")->valueint;
        thdata->tempMax= cJSON_GetObjectItem(data,"temp")->valueint;
        thdata->humiMax= cJSON_GetObjectItem(data,"humi")->valueint;
        thdata->smokeMax= cJSON_GetObjectItem(data,"somke")->valueint;
        thdata->sunshineMax= cJSON_GetObjectItem(data,"suns")->valueint;

        cJSON_Delete(data);//删除data
        cJSON_Delete(json);//删除json
        return 1;
    }
}